<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-row type="flex" justify="end">
        <el-button type="primary" size="small" @click="addFn('0','1')">添加权限</el-button>
      </el-row>
      <!-- 放置一个弹层 用来编辑新增节点 -->
      <el-dialog :title="formData.id?'编辑权限点':'新增权限点'" :visible="showDialog" @close="closeFn">
        <!-- 表单 -->
        <el-form ref="perForm" label-width="120px" :model="formData" :rules="rules">
          <el-form-item label="权限标题" prop="title">
            <el-input v-model="formData.title" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="confirmFn">确定</el-button>
            <el-button size="small" @click="closeFn">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-table :data="PermissionList" row-key="id" border default-expand-all :tree-props="{children: 'children'}">
        <el-table-column align="left" label="权限标题" prop="title" />
        <el-table-column align="center" label="权限标识" prop="code" />
        <el-table-column align="center" label="权限描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <!-- 因为项目中只有页面权限和按钮权限，不存在3级权限，所以把二级权限的添加按钮隐藏 -->
            <el-button v-if="row.type==1" type="primary" @click="addFn(row.id,'2')">添加子权限</el-button>
            <el-button v-if="assignPerm('edit_permission')" plain @click="updateFn(row.id)">编辑</el-button>
            <el-button v-if="assignPerm('del_permission')" type="danger" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getPermissionList, delPermissionList, addPermissionList, getTotal, updatePermissionList, getPermission } from '@/api/permissions'
import { listTreeData } from '@/utils'
export default {
  data() {
    return {
      PermissionList: [], // 权限列表
      formData: {
        title: '', // 标题
        code: '', // 标识
        description: '', // 描述
        type: '1', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '0' // 因为做的是树 需要知道添加到哪个节点下了
      },
      showDialog: false, // 控制弹窗显示
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '标题长度2-10位', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '标识不能为空', trigger: 'blur' },
          { min: 2, max: 20, message: '标识长度2-20位', trigger: 'blur' }
        ]
      },
      currentPage: 1,
      total: 0 // 总条数
    }
  },
  async created() {
    this.getPermissionList()
    this.getTotal()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      this.PermissionList = await getPermissionList({
        _limit: 1000,
        _start: 0
      })
      console.log(98, this.PermissionList)
      // 转树形结构
      this.PermissionList = listTreeData(this.PermissionList, '0')
      console.log(101, this.PermissionList)
      // this.getTotal()
    },
    // 获取总条数
    async getTotal() {
      this.total = await getTotal()
    },
    // 删除权限
    delFn(id) {
      this.$confirm('删除部门', '删除', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'info'
      }).then(async() => {
        await delPermissionList(id)
        this.$message.success('删除成功')
        this.getPermissionList()
      }).catch(() => {
        // 取消
      })
    },
    // 增加权限
    async  addFn(id, type) {
      this.showDialog = true
      console.log(127, id, type)
      this.formData.pid = id.toString()
      this.formData.type = type
    },
    // 点击弹窗确定按钮
    async confirmFn(id) {
      // 表单验证
      await this.$refs.perForm.validate()
      if (this.formData.id) {
      // 编辑
        await updatePermissionList(this.formData)
        this.$message.success('编辑成功')
      } else {
      // 增加
        await addPermissionList(this.formData)
        this.$message.success('增加成功')
      }
      // 关闭弹窗
      this.closeFn()
      // 更新页面
      this.getPermissionList()
    },
    // 关闭弹窗
    closeFn() {
      this.showDialog = false
      this.formData = {
        title: '', // 标题
        code: '', // 标识
        description: '', // 描述
        type: '1', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '0' // 因为做的是树 需要知道添加到哪个节点下了
      }
      // 清除校验
      this.$refs.perForm.resetFields()
    },
    // 编辑权限
    async updateFn(id) {
      this.showDialog = true
      this.formData = await getPermission(id)
    }
  }
}
</script>

<style scoped>
.el-button {
  margin: 0 10px 18px 0;
}
 </style>
